package io.renren.modules.live.controller;

import io.renren.common.utils.FastDFSClientUtils;
import io.renren.common.utils.R;
import io.renren.modules.live.constants.ResourcesConstants;
import io.renren.modules.live.entity.FanweAnchorEntity;
import io.renren.modules.live.entity.FanweUserEntity;
import io.renren.modules.live.entity.FanweVideoEntity;
import io.renren.modules.live.entity.ResourceBean;
import io.renren.modules.live.service.FanweAnchorService;
import io.renren.modules.live.service.FanweUserService;
import io.renren.modules.live.service.FanweVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.log4j.Log4j;
import lombok.extern.log4j.Log4j2;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author shaoqi
 * @Date 2020/11/21 0021 10:50
 * @Version 1.0
 */
@Log4j2
@Api(tags = "文件/头像/封面管理")
@RestController
@RequestMapping(value = "/file")
public class FileController {

    @Autowired
    private FanweUserService fanweUserService;
    @Autowired
    private FanweAnchorService anchorService;



    @ApiOperation("主播/用户根据id 文件/头像 上传接口")
    @RequestMapping(value={"/UploadFile"}, method= RequestMethod.POST,consumes = "multipart/form-data")
    public R anchorUploadFile(@RequestParam("muFile") MultipartFile muFile,
                        HttpServletRequest request,
                        @ApiParam(value = "userAndAnchorId",name = "主播/用户id")Long userAndAnchorId){

        String token = request.getHeader("token");
        String s = token.substring(0, 3);
        if(s.equals("usr")){
                ResourceBean fileUrlAndName = uploadFileUitl(muFile);
                //保存地址到用户数据库
                fanweUserService.addUserFile(userAndAnchorId,fileUrlAndName.getFileUrl());
                return R.ok("上传成功").put("fileUrlAndName",fileUrlAndName);
            }
        else if(s.equals("anc")){
                ResourceBean fileUrlAndName = uploadFileUitl(muFile);
                //保存地址到用户数据库
                anchorService.addAnchorFile(userAndAnchorId,fileUrlAndName.getFileUrl());
                return R.ok("上传成功").put("fileUrlAndName",fileUrlAndName);
            }
        return R.ok("上传成功");
    }


    private ResourceBean uploadFileUitl(MultipartFile file){
        String extName = "";
        String fileName = "";
        String originalFilename = file.getOriginalFilename();
        if(originalFilename.contains(".")) {
            //拆分文件路径
            String[] fileArray = originalFilename.split("\\.");
            //获取文件扩展名
            extName = fileArray[1];
            //获取文件名
            fileName = fileArray[0];
        }else {
            fileName = originalFilename;
        }
        byte[] bytes = null;
        try {
            bytes = file.getBytes(); //将文件转换成字节流形式
        } catch (IOException e) {
            log.info(e);
        }
        //调用上传文件的具体方法
        String fileId= FastDFSClientUtils.upload(bytes,extName);
        return new ResourceBean(ResourcesConstants.getResourcesUrl(fileId), fileName);
    }


}